//
//  WBTitleButton.swift
//  mySwiftVV
//
//  Created by hu yr on 2017/1/13.
//  Copyright © 2017年 terry. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {

    //重载构造函数
    ///title 如果是 nil,就显示首页
    /// 如果不为nil,显示title和箭头图像
    init(title:String?) {
        super.init(frame: CGRect())
        
        //1.判断 title 是否为nil
        if title == nil{
            setTitle("首页", for: [])
            
        }else{
            setTitle(title! + " ", for: [])
            
            //3.设置图像
            setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        }
        
        //2,设置字体和颜色
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
        //设置大小
        sizeToFit()
        
        
        
        
       
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel, let imageView = imageView else {
            
            return
        }
        //将lebel 的 x 向左移动 imageView的宽度
        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
        
        //将imageView的 x 向右移动 label 的宽度
        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        
        
    }
}
